package 算法回顾;

import java.util.Stack;

/**
 * @description: 小白白刷题
 * @author: 小白白
 * @create: 2021-09-08
 **/

public class JZ09用两个栈实现队列 {

    static class CQueue {

        // 入列用
        private Stack<Integer> stack1;
        // 出列用
        private Stack<Integer> stack2;

        public CQueue() {
            this.stack1 = new Stack<>();
            this.stack2 = new Stack<>();
        }

        public void appendTail(int value) {
            this.stack1.add(value);
        }

        public int deleteHead() {
            if (!this.stack2.isEmpty()) {
                return this.stack2.pop();
            }
            while (!this.stack1.isEmpty()) {
                this.stack2.add(this.stack1.pop());
            }
            return this.stack2.isEmpty() ? -1 : this.stack2.pop();
        }

        public static void main(String[] args) {
            CQueue cQueue = new CQueue();
            cQueue.appendTail(1);
            cQueue.appendTail(2);
            cQueue.appendTail(3);
            cQueue.appendTail(4);
            cQueue.appendTail(5);
            System.out.println(cQueue.deleteHead());
            cQueue.appendTail(6);
            for (int i = 0; i < 7; i++) {
                System.out.println(cQueue.deleteHead());
            }
        }

    }


}
